<%-- 
    Document   : list
    Created on : Nov 16, 2014, 1:27:34 PM
    Author     : Hai
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="Theme/css/Menu.css" rel="stylesheet" type="text/css"/>
        <title>JSP Page</title>
    </head>
    <body>
        <%@include file="header.jsp" %>
        <h1>View!</h1>
        <button type="button" value="New Post"><a href="/post.jsp">New Post</a></button>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Title</th>
                    <th>Posted by</th>
                    <th>Day</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th><a href=
                           <%  String sTable = request.getParameter("where");
                               String sPage = request.getParameter("p");
                               int numPage = 0;

                               if (sPage != null) {
                                   numPage = Integer.parseInt(sPage);
                               }

                               if (numPage * 10 <= 10) {
                                   out.print("?where=" + sTable + "&p=" + (0) + "");
                               } else {
                                   out.print("?where=" + sTable + "&p=" + (numPage - 10) + "");
                               }
                           %>
                           >Prev</a></th>
                    <th>
                        <%
                            out.print(Database.GetData.numPage(sTable, "" + (numPage)));
                        %>
                    </th>
                    <th><a href=
                           <%
                               if (numPage >= (Database.GetData.numRow(sTable) / 10)) {
                                   out.print("?where=" + sTable + "&p=" + (numPage) + "");
                               } else {
                                   out.print("?where=" + sTable + "&p=" + (numPage + 10) + "");
                               }
                           %>
                           >Next</a></th>
                    <th></th>
                </tr>
            </tfoot>
            <tbody>

                <%
                    String data = Database.GetData.Table(sTable, numPage);

                    String[] row = data.split("/n");

                    for (String val : row) {
                        String[] col = val.split("/r");
                        if (col.length > 1) {
                            out.print("<tr>");
                            out.print("<th><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + col[0] + "</a></th>");
                            out.print("<th><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + col[1] + "</a></th>");
                            out.print("<th><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + Database.GetData.id2Name(col[2]) + "</a></th>");
                            out.print("<th><a href='/view.jsp?where=" + sTable + "&id=" + col[0] + "'>" + col[3] + "</a></th>");
                            out.print("</tr>");
                        }
                    }

                %>
            </tbody>
        </table>

    </body>
</html>
